package ltd.hxya.novel.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ltd.hxya.novel.admin.entity.Role;
import ltd.hxya.novel.admin.entity.UserRole;
import ltd.hxya.novel.admin.service.IRoleService;
import ltd.hxya.novel.admin.service.UserRoleService;
import ltd.hxya.novel.admin.vo.RoleVo;
import ltd.hxya.novel.admin.vo.UserVo;
import ltd.hxya.novel.common.bean.Result;
import ltd.hxya.novel.common.utils.BaseUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hty
 * @since 2022-10-04
 */
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 根据条件查询角色信息集合
     */
    @PreAuthorize("@expression.hasAuthority('admin:role:list')")
    @GetMapping("/roleList")
    public Result<Page<Role>> roleList (Role role){

        Page<Role> rolePage = roleService.roleList(role);
        return Result.success(rolePage);
    }

    /**
     * 根据角色id查询角色信息
     */
    @PreAuthorize("@expression.hasAuthority('admin:role:info')")
    @GetMapping("/info/{roleId}")
    public Result<RoleVo> info(@PathVariable Integer roleId) throws ExecutionException, InterruptedException {
        RoleVo roleVo = roleService.roleInfo(roleId);
        return Result.success(roleVo);
    }

    /**
     * 根据角色id查询出已经授予该角色的管理员
     */
    @PreAuthorize("@expression.hasAuthority('*')")
    @GetMapping("/grantedUser")
    public Result<Page<UserVo>> grantedUser(UserVo userVo){
        //查询已经授予权限的信息
        Page<UserVo> userVoPage = roleService.grantedUser(userVo);
        return Result.success(userVoPage);
    }

    /***
     * 根据角色id查询出未授予该角色的管理员
     */
    @PreAuthorize("@expression.hasAuthority('*')")
    @GetMapping("/ungrantedUser")
    public Result<Page<UserVo>> ungrantedUser(UserVo userVo){
        Page<UserVo> userVoPage = roleService.ungrantedUser(userVo);
        return Result.success(userVoPage);
    }

    /**
     * 批量对用户授予角色
     */
    @PreAuthorize(("@expression.hasAuthority('*')"))
    @PutMapping("/grantRole")
    public Result grantRole(Integer roleId,Integer[] userIds){

        return BaseUtils.hasRow(roleService.grantRole(roleId,userIds));
    }

    /**
     * 取消角色授权
     */
    @PreAuthorize("@expression.hasAuthority('*')")
    @DeleteMapping("/revoke")
    public Result revoke(UserRole userRole){
        return BaseUtils.hasRow(userRoleService.revoke(userRole));
    }

    /**
     * 批量取消授权
     */
    @PreAuthorize("@expression.hasAuthority('*')")
    @DeleteMapping("/revokeAll")
    public Result revokeAll(Long roleId,Long[] userIds){
        return BaseUtils.hasRow(roleService.revokeAll(roleId,userIds)) ;

    }

    /**
     * 根据id删除角色
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer[] ids){
        roleService.deleteById(ids);
        return Result.success();
    }

    /**
     * 添加的处理
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    @PreAuthorize("@expression.hasAuthority('*')")
    public Result addRole(@Validated @RequestBody Role role){
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 修改的处理
     * @param role
     * @return
     */
    @PutMapping("/updateRole")
    @PreAuthorize("@expression.hasAuthority('*')")
    public Result updateRole(@Validated @RequestBody Role role, HttpServletRequest request){
        roleService.updateRole(role,request);
        return Result.success();
    }

    @GetMapping("/getRoleAll")
    @PreAuthorize("@expression.hasAuthority('*')")
    public Result getRoleAll(){
        List<Role> roleAll = roleService.getRoleAll();
        return Result.success(roleAll);
    }

}
